// pages/user/userCenter/userCenter.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        statusBarHeight: 0,
        messageInfo: null,
        userInfo: null,
        orderInfo: null,
        shopInfo: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getStatusHeight()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getMessageInfo()
        this.getUserInfo()
        this.getOrderInfo()
        this.getShopInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            nowDate: '',
            statusBarHeight: 0,
            messageInfo: null,
            userInfo: null,
            orderInfo: null,
            shopInfo: null
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    getStatusHeight: function () {
        const that = this
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    statusBarHeight: res.statusBarHeight + 8
                })
            }
        })
    },

    getMessageInfo: function () {
        const that = this
        let data = {}
        common.post('System/send', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                messageInfo: res
            })
        }).catch(function (e) { })
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function (e) { })
    },

    empowerUserInfo: function (e) {
        console.log(e)
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            code: wx.getStorageSync('code'),
            phone: wx.getStorageSync('userPhone'),
            wx_sm: 1,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
        }
        common.post('Login/phone_unionid', data).then(function (res) {
            wx.hideLoading()
            wx.login({
                success(loginRes) {
                    wx.setStorageSync('code', loginRes.code)
                }
            })
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) { }
                })
                return
            }
            that.getUserInfo()
        }).catch(function (e) { })
    },

    closeTips: function () {
        const that = this
        let data = {
            is_collect: 1
        }
        common.post('User/up_info', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.getUserInfo()
        }).catch(function (e) { })
    },

    getOrderInfo: function () {
        const that = this
        let data = {}
        common.post('Orderlist/order_num', data).then(function (res) {
            that.setData({
                orderInfo: res
            })
        }).catch(function (e) { })
    },

    getShopInfo: function () {
        const that = this
        let data = {}
        common.post('Storequota/get_store_quota', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopInfo: res
                })
            }
        }).catch(function (e) { })
    },

    exitLogin: function () {
        wx.login({
            success(res) {
                wx.setStorageSync('code', res.code)
            }
        })
        wx.setStorageSync('prePageUrl', '/pages/user/userCenter/userCenter')
        wx.removeStorageSync("token")
        wx.redirectTo({
            url: '/pages/user/login/login',
        })
    },

    goCollege: function () {
        wx.navigateTo({
            url: '/pages/source/sourceIndex/sourceIndex'
        })
    }
})